#include "videoform.h"
#include "ui_videoform.h"
#define VOL 50//默认音量

bool if_reload=false;
bool if_play=false;
//定义与滑块有关的全局变量
QTimer *timer;
int maxValue=1000;//设置进度条最大1000

videoForm::videoForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::videoForm)
{
    ui->setupUi(this);
    ui->volume->setValue(VOL);

    //播放器对象，播放队列对象，显示对象
    qPlayer=new QMediaPlayer;
    qPlayerList=new QMediaPlaylist;
    qVideoWidget=new QVideoWidget(ui->label);// 播放窗口在label中进行播放
    qVideoWidget->resize(ui->label->size());//为播放器指定大小
    qPlayer->setPlaylist(qPlayerList);//设置播放列表
    qPlayer->setVideoOutput(qVideoWidget);//设置播放器的显示窗口

    //设置播放模式
    qPlayerList->setPlaybackMode(QMediaPlaylist::Loop);
    //设置播放器的播放列表
    qPlayer->setPlaylist(qPlayerList);

    connect(qPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(setPlayTime()));


}

videoForm::~videoForm()
{
    delete ui;
}

void videoForm::on_openbutton_clicked()
{
    //ui->bar->setValue(0);
    //文件查找框
    //    QStringList fileNames = QFileDialog::getOpenFileNames(this,"文件选择","C:/Users/1501231/Videos","allfiles(*.*);;"
    //                                                                                                "mp3(*.mp3);;"
    //                                                                                                "mp4(*.mp4);;");//文件路径要用"/"，不能用"\"和windows下不同。

    // 若需过滤特定格式（如 mp3/mp4）

    QString dirPath = "C:/Users/1501231/Videos";  // 指定目标文件夹
    QDir directory(dirPath);
    QStringList filters = {"*.mp3", "*.mp4"};
    QFileInfoList  fileInfoList  = directory.entryInfoList(filters, QDir::Files);
    QStringList fullPaths;  // 存储完整路径
    QStringList fileNames;  // 存储文件名（用于UI显示）

    foreach (const QFileInfo& fileInfo, fileInfoList) {
        fullPaths.append(fileInfo.absoluteFilePath()); // 获取完整路径 [6,7](@ref)
        fileNames.append(fileInfo.fileName());         // 仅文件名（用于UI）
    }

    //重置timer，更新flag
    if(if_reload)
    {
        delete timer;
    }
    if_reload=true;

    //添加文件
    foreach(QString const& filePath,fullPaths){//在QStringList对象中循环，可以将fileName添加到准备播放任务中
        QUrl url(filePath);
        qPlayerList->addMedia(url);
    }
    ui->listWidget->addItems(fileNames);
}

//计算时间
void videoForm::onTimerOut()
{
    ui->bar->setValue(qPlayer->position()*maxValue/qPlayer->duration());
}

void videoForm::on_playbutton_clicked()
{
    qPlayer->setVolume(ui->volume->value()); //设置音量
    if(if_play)
    {
        delete timer;
    }
    if_play = true;
    //播放时，将timer连接到OnTimerOUT槽函数，获取最大时间
    ui->bar->setRange(0,maxValue);  //设置最大最小值
    timer = new QTimer();
    timer->setInterval(1000);
    timer->start();
    //将timer连接至onTimerOut
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
    qPlayer->play();
}

void videoForm::on_stopbutton_clicked()
{
    qPlayer->stop();

}

void videoForm::on_lastbutton_clicked()
{
    int row=ui->listWidget->currentRow();
    if(row==0)
        row=ui->listWidget->count();
    row--;

    ui->listWidget->setCurrentRow(row);
    on_listWidget_itemDoubleClicked(ui->listWidget->currentItem());
}

void videoForm::on_suspendbutton_clicked()
{
    qPlayer->pause();
}

void videoForm::on_nextbutton_clicked()
{
    int row=ui->listWidget->currentRow();
    if(row==ui->listWidget->count()-1)
        row=0;
    else
        row++;

    ui->listWidget->setCurrentRow(row);

    on_listWidget_itemDoubleClicked(ui->listWidget->currentItem());
}


void videoForm::on_bar_sliderMoved(int position)
{
    //暂停计时器，拖动过程中不改变slider的值
    timer->stop();
    qPlayer->setPosition(position*qPlayer->duration()/maxValue);

}

void videoForm::on_bar_sliderPressed()
{
    qPlayer->setPosition(ui->bar->value()*qPlayer->duration()/maxValue);
}

void videoForm::on_bar_sliderReleased()
{
    timer->start();
}

void videoForm::on_volume_sliderMoved(int position)
{
    qPlayer->setVolume(position);
}


void videoForm::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    int row = ui->listWidget->currentRow();
    qPlayerList->setCurrentIndex(row);


    qPlayer->setVolume(ui->volume->value());    //设置播放音量
    on_playbutton_clicked();

}


void videoForm::getduration()
{
    playtime = qPlayer->duration();
    QString tm= settime(playtime);
    qDebug()<<tm ;
}

QString videoForm::settime(int time)
{
    int h,m,s;
    time /= 1000;  //获得的时间是以毫秒为单位的
    h = time/3600;
    m = (time-h*3600)/60;
    s = time-h*3600-m*60;
    return QString("%1:%2:%3").arg(h).arg(m).arg(s);
}

